#!/usr/bin/python3
import json
import sys

from catalog_update.upgrade_strategy import datetime_versioning


def newer_mapping(image_tags):
    key = list(image_tags.keys())[0]
    tags = {t.strip('RELEASE.'): t for t in image_tags[key] if t.startswith('RELEASE.') and t.endswith('Z')}
    version = datetime_versioning(list(tags), '%Y-%m-%dT%H-%M-%SZ')
    if not version:
        return {}

    return {
        'tags': {key: tags[version]},
        'app_version': version.split('T')[0],
    }


if __name__ == '__main__':
    if len(sys.argv) != 2:
        exit(1)

    print(json.dumps(newer_mapping(json.loads(sys.argv[1]))))
